package cn.com.zero.base.config;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description 自动获取服务的IP地址，以便Logback使用
 * @createTime 2022/7/12 17:11
 */
public class LogbackIpConverter extends ClassicConverter {
    private static final Logger logger = LoggerFactory.getLogger(LogbackIpConverter.class);
    private static final String DEFAULT_IP_ADDRESS = "0.0.0.0.";
    private static String ipAddress = DEFAULT_IP_ADDRESS;

    static {
        try {
            // 获取当前服务的IP地址
            ipAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {

        }
    }

    @Override
    public String convert(ILoggingEvent iLoggingEvent) {
        return ipAddress;
    }
}
